package com.dmiot.mycustomview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

//https://blog.csdn.net/fictionss/article/details/78285167
//https://blog.csdn.net/yuanheng19930119/article/details/88055225
//https://blog.csdn.net/yuanheng19930119/article/details/88055355
// https://blog.csdn.net/yuanheng19930119/article/details/88055689
//https://www.jianshu.com/p/5a71014e7b1b
//https://www.jianshu.com/p/c151efe22d0d

public class SimpleView extends View {
    private final static String TAG = SimpleView.class.getSimpleName();
    //画笔
    private Paint mPaint;
    private RectF oval;

    //事件处理
    private EventHandle mEventHandle;
    //鼠标按下位置
    private int startX,startY;
    //按下鼠标时控件的位置
    private int startLeft,startTop;
    //状态栏高度
    int statusHeight = 0;

    public SimpleView(Context context) {
        super(context);
        init();
    }

    public SimpleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SimpleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SimpleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init(){
        mPaint = new Paint();
        //设置是否使用抗锯齿功能，会消耗较大资源，绘制图形速度会变慢。
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(30.0f);
        oval=new RectF();
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        Log.e(TAG, "onMeasure--widthMode-->" + widthMode);
        switch (widthMode) {
            case MeasureSpec.EXACTLY:
                //精确值模式，当控件的layout_width和layout_height属性指定为具体数值或match_parent时。
                break;
            case MeasureSpec.AT_MOST:
                //最大值模式，当空间的宽高设置为wrap_content时。
                break;
            case MeasureSpec.UNSPECIFIED:
                //未指定模式，View想多大就多大，通常在绘制自定义View时才会用。
                break;
        }
        //取最小边为控件的宽高的最小值
        int minWidth=widthSize>heightSize?heightSize:widthSize;
        setMeasuredDimension(minWidth,minWidth);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.GRAY);
        // FILL填充, STROKE描边,FILL_AND_STROKE填充和描边
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        int with = getWidth();
        int height = getHeight();
        Log.e(TAG, "onDraw---->" + with + "*" + height);
        float radius = with / 4;
        canvas.drawCircle(with / 2, with / 2, radius, mPaint);
        mPaint.setColor(Color.RED);
        oval.set(with / 2 - radius, with / 2 - radius, with / 2
                + radius, with / 2 + radius);//用于定义的圆弧的形状和大小的界限
        int sweepAngle=120;
        canvas.drawArc(oval, 0, -sweepAngle, true, mPaint);  //根据进度画圆弧
        double percent=sweepAngle/360.0;
        //设置文本颜色
        mPaint.setColor(Color.WHITE);
        //绘制文本百分比数据
        canvas.drawText(String.format("%.2f",percent)+"%",(float)(with/2+radius*Math.cos(sweepAngle*Math.PI/360)/4)
                ,(float)(with/2-radius*Math.sin(sweepAngle*Math.PI/360)/3),mPaint);
        canvas.drawText(String.format("%.2f",1-percent)+"%",(float)(with/2-radius*Math.cos(sweepAngle*Math.PI/360))
                ,(float)(with/2+radius*Math.sin(sweepAngle*Math.PI/360)/3),mPaint);
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {


        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:

                startX=(int)event.getRawX();
                startY=(int)event.getRawY();
                startLeft=(int)(startX-event.getX());
                /**
                 * 这里startTop计算有些偏离，原因在于计算时加入了标题栏和状态栏的高度
                 * 注意：要是你的Activity没有去掉标题栏，这里还要去掉标题栏的高度
                 */
                startTop= (int)(startY-event.getY())-statusHeight;//减去状态栏高度
                break;
            case MotionEvent.ACTION_MOVE:
                if(mEventHandle!=null)
                {
                    mEventHandle.onTouchEvent(event);
                }else{
                    int disX=(int)event.getRawX()-startX;//计算偏移的X坐标
                    int disY=(int)event.getRawY()-startY;//计算偏移的Y坐标;
                    int left=startLeft+disX;
                    int top=startTop+disY;
                    //更新控件位置
                    layout(left,top,left+getWidth(),top+getHeight());
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        //返回true表示不消耗此事件，事件继续传递，返回flase表示事件消耗
        return true;
    }

    public void setmEventHandle(EventHandle mEventHandle) {
        this.mEventHandle = mEventHandle;
    }

    interface EventHandle{
        public void onTouchEvent(MotionEvent event);
    }
}
